package com.mmall.service.Impl;

import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("iCategoryService")
@Slf4j
public class CategoryServiceImpl implements ICategoryService {


    //private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    CategoryMapper categoryMapper;
    public ServerResponse addCategory(String categoryName,Integer parentId){
        if (parentId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("添加产品错误");
        }

        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);

        int rowCount = categoryMapper.insert(category);
        if (rowCount > 0){
            return  ServerResponse.createBySuccess("添加品类成功");
        }

        return  ServerResponse.createByErrorMessage("添加品类失败");

    }

    public ServerResponse updateCategoryName(Integer categoryId,String categpryName){
        if (categoryId == null || StringUtils.isBlank(categpryName)){
            return ServerResponse.createByErrorMessage("更新品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categpryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if(rowCount > 0 ){
            return  ServerResponse.createBySuccessMessage("更新品类成功");
        }
        return  ServerResponse.createByErrorMessage("更新品类失败");
    }


    public ServerResponse<List<Category>> getChilderParallelCategory(Integer categoryId){
        List<Category> categoryList = categoryMapper.getChilderParallelCategory(categoryId);
        if (CollectionUtils.isEmpty(categoryList)){
            log.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }


    /**
     * 获取节点及其子节点
     */
    public ServerResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryId){
        Set<Category> set = new HashSet<Category>();
        findChildCategory(set,categoryId);
        List<Integer> list = new ArrayList();
        if (categoryId != null){
            for (Category category : set){
                list.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(list);
    }

    public  Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null){
            categorySet.add(category);
        }
        List<Category> list =  categoryMapper.getChilderParallelCategory(categoryId);
        for (Category category1 : list){
            findChildCategory(categorySet,category1.getId());
        }
        return  categorySet;
    }
}
